<?php

include dirname(dirname(dirname(__FILE__))) . '/include/common.php';

class webServiceBudgetList extends core {

   public function process() {
        $limit = $this->limit;
        $page = intval($_POST['page']);
        $user_id = intval($_POST['user_id']);
        if ($page < 1) {
            $page = 1;
        }
        $from = ($page - 1) * $limit;
        $total = 0;

        $data = $this->db->get_results("SELECT * FROM budget_items WHERE user_id={$user_id} LIMIT $from,$limit;");
        $total = $this->db->get_row("SELECT count(item_id) as total FROM budget_items WHERE user_id={$user_id} ;");
        
        if (ceil($total->total / $limit) <= $page) {
            $end_paging = 1;
        } else {
            $end_paging = 0;
        }

        $item_list = '';
        foreach ($data as $budget) {
              $item_list.=' <div class="wd-budget-list-item">
                                <span class="wd-budget-list-item-txt">'.$budget->item_name.'</span>
                                <span class="wd-budget-list-item-number">$'.  number_format($budget->item_amount,2).'
                                    <img src="img/edit-icon.png"  class="wd-budget-edit-ico wd-budget-edit-item" rel="'.$budget->item_id.'"/>
                                    <img src="img/delete-icon.png" class="wd-budget-delete-ico" rel="'.$budget->item_id.'"/>
                                </span>
                            </div>';
        }
        if ($item_list == '') {
            $item_list = '<div style="margin: 0 20px;color: #876d63;">No item was found.</div>';
        }
        $result_spent = $this->db->get_row("SELECT SUM(item_amount) AS total_spent, budget_goal FROM budget_items,users WHERE user_id=$user_id AND budget_items.user_id = users.id");
        $spent = $result_spent->total_spent;
        $goal = $result_spent->budget_goal;
        $unused_money = $goal - $spent;
        $notice_msg='';
        if($unused_money < 0){
            $notice_msg = "Your spending is higher than your goal at this time";
        }
        $format_unused_money = strval(number_format($unused_money,2));
        $length = strlen($format_unused_money);
        $str_int_unused_money = strval(intval($unused_money));
        $html_unused_money ='';
        if(strlen($str_int_unused_money)%3==0){
            $html_unused_money .='<span class="wd-number-item">0</span>';
        }
        
        for($i=0;$i<$length;$i++){
            if($format_unused_money[$i]==","){
                $html_unused_money.='<span class="wd-number-item-space">,</span>';
            }elseif($format_unused_money[$i]=="."){
                $html_unused_money.='<span class="wd-number-item-dot">.</span>';
            }else{
                $html_unused_money.='<span class="wd-number-item">'.$format_unused_money[$i].'</span>';
            }
        }

        $this->_JSONResponse(array('error' => 0, 'item_list' => $item_list,
            'goal'=>"$ ".number_format($goal),'spent'=>"$ ".number_format($spent),
            "unused_money"=> $html_unused_money,
            "notice_msg"=>$notice_msg,
            'page' => $page + 1, 'end_paging' => $end_paging));
    }
  

}

$processor = new webServiceBudgetList();
$processor->process();
?>